package com.zjsru.service.Impl;

import com.zjsru.pojo.User;
import com.zjsru.clients.OrderClient;
import com.zjsru.mapper.UserMapper;
import com.zjsru.pojo.Order;
import com.zjsru.pojo.Result;
import com.zjsru.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Component
public class UserServiceImpl implements UserService {
    @Qualifier("com.zjsru.clients.OrderClient")
    @Autowired
    OrderClient orderClient;
    @Autowired
    private UserMapper userMapper;

    public User queryUserById(Long user_Id) {
        User user = userMapper.findById(user_Id);
        Result result = orderClient.queryOrderByUserId(user_Id);
        if (result.getCode() == 200) {
            List<Order> orderList = (List<Order>) result.getData();
            user.setOrderList(orderList);
        }else{
            user.setMessage(result.getMessage());
        }
        return user;
    }

    @Override
    public User checkLogin(String username, String password) {
        return userMapper.checkLogin(username, password);
    }

    @Override
    public void register(String username, String password) {
        userMapper.register(username, password);
    }

    @Override
    public User queryUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

}
